#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Purpose: Test hydroplatform.model.export
# Created: 02/24/2009
# $Id $
# Copyright (c) 2008,2009 by University College London
# Authors:
# Didrik Pinte --<dpinte@dipole-consulting.com>
#
# This program is free software under the GPL (>=v2)
# Read the file COPYING coming with HydroPlatform for details.

import cStringIO
import logging
import os
import unittest
import wx

import support
support.init_hydroplatform()

from hydroplatform.extension.export import excelexport
from hydroplatform.extension.export import textexport
from hydroplatform.extension.iras import irasexport
from hydroplatform.extension.export import otypes_export
from hydroplatform.model.network import Node, Project, NodeType, Network
from hydroplatform.model.export import Exporter, ExporterRegistry, ExportError
from hydroplatform.model.database import get_session
from hydroplatform.ui.export import ExportFrame

logging.basicConfig()
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

class TestExportRegistry(unittest.TestCase):
    
    def test_registry(self):        
        registry = ExporterRegistry()
        init_count = len(registry.available_exporters())
        e = Exporter()
        # register an exporter
        registry.register(e)
        self.assertEqual(len(registry.available_exporters()), init_count+1)
        # check that registering two times the same exporter does not store 
        # it twice
        registry.register(e)
        self.assertEqual(len(registry.available_exporters()), init_count+1)
        # check the borg pattern does work fine
        borg = ExporterRegistry()
        self.assertEqual(len(borg.available_exporters()), init_count+1)
        

class TestBasicExporter(unittest.TestCase):

    def setUp(self):
        self.session = support.get_dbsession()
        (self.nodetypes, self.nodes, self.links, self.network) = \
            support.load_sample_network1(self.session) 
        self.filename = "test.network"
    
    def test_basicexporter(self):
        e = Exporter()
        e.export(self.network, self.filename, self.session)
        f = open(self.filename, 'r')
        stream = f.read()
        f.close()
        self.assertTrue( len(stream) > 0)
        
    def tearDown(self):
        # remove created file if existing
        if os.path.exists(self.filename):
            os.remove(self.filename)
            
            
class TestTextExporter(unittest.TestCase):

    def setUp(self):
        self.session = support.get_dbsession()
        (self.nodetypes, self.nodes, self.links, self.network) = \
            support.load_sample_network1(self.session) 
        self.filename = "/home/did/tmp/test.network"
    
    def test_basicexporter(self):
        e = textexport.TextExport()
        e.export(self.network, self.filename, self.session)
        f = open(self.filename, 'r')
        stream = f.read()
        f.close()
        self.assertTrue( len(stream) > 0)
        
    def tearDown(self):
        # remove created file if existing
        #if os.path.exists(self.filename):
        #    os.remove(self.filename)            
        pass
        

class TestExcelExporter(unittest.TestCase):
    def setUp(self):
        self.session = support.get_dbsession()
        (self.nodetypes, self.nodes, self.links, self.network) = \
            support.load_sample_network1(self.session) 
        self.filename = "test.xls"
    
    def test_textexporter(self):
        e = excelexport.ExcelExporter()
        e.export(self.network, self.filename, self.session)
        
    def _tearDown(self):
        # remove created file if existing
        if os.path.exists(self.filename):
            os.remove(self.filename)
            
            
class TestTextExporter(unittest.TestCase):
    def setUp(self):
        self.inputfile = "/home/did/tmp/hydroplatform.spadula.db"
        self.session = get_session('sqlite:///%s' % self.inputfile)
        self.filename = "/home/did/tmp/test.gms"
        self.project =self.session.query(Project).filter(\
            Project.name == u"p2").first()
        self.network = self.project.networks[0]         
    
    def test_textexporter(self):
        e = textexport.TextExport()
        e.export(self.network, self.filename, self.session)
        
    def _tearDown(self):
        # remove created file if existing
        #if os.path.exists(self.filename):
        #    os.remove(self.filename)
        pass


class TestExcelSampleFileExporter(unittest.TestCase):
    """
    Tests the official network export
    """
    def setUp(self):
        """
        Initialise the connection to a database with an IRAS system
        """
        self.inputfile = "/home/did/tmp/hydroplatform_jhexp.db"
        self.session = get_session('sqlite:///%s' % self.inputfile)
        self.filename = "/home/did/tmp/test_exp.xls"
        self.project = self.session.query(Project).first()
        self.network = self.project.networks[0] 
        
    def test_excelexporter(self):
        e = excelexport.ExcelExporter()
        e.export(self.network, self.filename, self.session)

        
    def _tearDown(self):
        self.session.close()
            
class TestIrasExporter(unittest.TestCase):
    """
    Test the IRAS exporter with a sample db file
    """
    
    def setUp(self):
        """
        Initialise the connection to a database with an IRAS system
        """
        basedir = r"C:\Users\dpinte\Documents\tmp"
        #self.inputfile = os.path.join(basedir, "hydroplatform.db")
        self.inputfile = "/home/did/tmp/hydroplatform.db"
        self.session = get_session('sqlite:///%s' % self.inputfile)
        self.filename = os.path.join(basedir, "test.inp")
        self.project =self.session.query(Project).filter(\
            Project.name == u"London").first()
        self.network = self.project.networks[0] 
        
    def test_irasexport(self):
        if self.network is None:
            self.fail()
        e = irasexport.IrasExporter()
        e.export(self.network, self.filename, self.session)
        self.session.commit()
        
    def tearDown(self):
        """
        Should do some cleanup there
        """
        self.session.close()

class TestObjectTypeExporter(unittest.TestCase):
    """
    Test the object type exporter with a sample db file
    """
    
    def setUp(self):
        """
        Initialise the connection to a database with an IRAS system
        """
        #self.inputfile = "/home/did/tmp/hydroplatform_iras.db"
        self.inputfile = "/home/did/tmp/hydroplatform.db"
        #basedir = r"C:\Users\dpinte\Documents\tmp"
        #self.inputfile = os.path.join(basedir, "hydroplatform.db")        
        self.session = get_session('sqlite:///%s' % self.inputfile)
        self.filename = "/home/did/tmp/hp_object_types.log"
        #self.filename = os.path.join(basedir, "hp_object_types.log")        
        self.project =self.session.query(Project).filter(\
            Project.name == u"London").first()
        self.network = self.project.networks[0] 
        
    def test_object_type_export(self):
        if self.network is None:
            self.fail()
        e = otypes_export.ExportObjectTypes()
        e.export(self.network, self.filename, self.session)
        self.session.commit()
        
    def tearDown(self):
        """
        Should do some cleanup there
        """
        self.session.close()



    
if __name__=='__main__':
    reg = ExporterRegistry()
    print reg.available_exporters()
    session = support.get_dbsession()
    support.load_sample_network1(session)
    project = session.query(Project).first()
    context = support.Context()
    context.session.hpproject = project
    context.session.dbsession = session
    app = wx.App()
    eframe = ExportFrame(None, context)
    eframe.Show()
    app.MainLoop()
    #unittest.main()